package org.zfm.base.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zfm.base.service.IVerifyCodeService;
import org.zfm.user.domain.dto.SmsCodeDto;
import org.zfm.util.data.AjaxResult;

import java.util.Map;

@RestController
@RequestMapping("/verify")
public class VerifyController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 随机生成图形验证码 并将验证码存入redis中，key就用前端传来的key
     * @param imageKey
     * @return
     */
    @GetMapping("/imageCode/{imageKey}")
    public AjaxResult getAuditCode(@PathVariable String imageKey){
        try {
            String codeImage = verifyCodeService.getCodeImage(imageKey);
            return AjaxResult.getBean().setResult(codeImage);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage("获取图形验证码失败！"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult getSmsCode(@RequestBody SmsCodeDto dto){
        try {
            verifyCodeService.getSmsCode(dto);
            return AjaxResult.getBean().setMessage("发送验证码成功，请及时查收！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage(e.getMessage()).setSuccess(false);
        }
    }

    @PostMapping("/toBind/smsCode")
    public AjaxResult getBindSmsCode(@RequestBody Map<String,String> map){
        try {
            System.out.println(map.get("phone"));
            verifyCodeService.getBindSmsCode(map.get("phone"));
            return AjaxResult.getBean().setMessage("验证码发送成功，请在五分钟内使用");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage(e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/smsLoginCode/{phone}")
    public AjaxResult smsLoginCode(@PathVariable("phone") String phone){
        try {
            verifyCodeService.smsLoginCode(phone);
            return AjaxResult.getBean().setMessage("验证码发送成功，请在五分钟内使用");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage(e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/getPasswordSmsCode/{phone}")
    public AjaxResult getPasswordSmsCode(@PathVariable("phone") String phone){
        try {
            verifyCodeService.getPasswordSmsCode(phone);
            return AjaxResult.getBean().setMessage("验证码发送成功，请在五分钟内使用");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage(e.getMessage()).setSuccess(false);
        }
    }

}
